﻿using JSLCloud.App.Mes.Extentions;
using JSLCloud.App.Mes.Service;
using JSLCloud.Extentions;
using JSLCloud.Framework;
using JSLCloud.Models;
using JSLCloud.Utilities.AutoMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System;
using System.IO;
using System.Reflection;

namespace JSLCloud.App.Mes
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers().AddNewtonsoftJson();

            services.AddMvc(options =>
            {
                options.Filters.Add<YFAuthorizationFilterAttribute>();
            });
            //权限过滤器 读取当前模块的密钥
            TokenManager.SecurityKey = Configuration.GetSection("SecurityKey").Value.ToString();

            //配置Swagger参数信息
            services.AddSwaggerGen(sg =>
            {
                sg.SwaggerDoc("JSLMes", new OpenApiInfo
                {
                    Title = "JSLMes",
                    Version = "1.0.0.1"
                });
                //当前项目的xml注释文件
                var currProjectXmlFile = Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml");
                sg.IncludeXmlComments(currProjectXmlFile);

                //JSLCloud.Models
                var modelXmlFile = Path.Combine(AppContext.BaseDirectory, "JSLCloud.Models.xml");
                sg.IncludeXmlComments(modelXmlFile);
            });

            //配置系统参数信息
            services.Configure<AppConfig>(Configuration.GetSection("AppConfig"));

            //注册当前程序集所有标记了UseDI的服务
            services.AutoRegisterServicesFromAssembly(Assembly.GetExecutingAssembly().GetName().Name);
            services.AutoRegisterServicesFromAssembly("JSLCloud.Framework");

            //启动后台线程
            var provider = services.BuildServiceProvider();
            var threadService = provider.GetService<IBackThreadService>();
            services.AddBackThread(threadService);


            //注册AutoMapper服务以及创建映射关系
            services.AddAutoMapper(config =>
            {
                //注册当前程序集所有标记UserAutoMapper的服务
                var mapperlist = AutoMapperUtil.GetMapperList(Assembly.GetExecutingAssembly().GetName().Name);
                foreach (var item in mapperlist)
                {
                    config.CreateMap(item.Key, item.Value);
                }
            });

            //跨域
            services.AddCors(options =>
            {
                options.AddPolicy("AllowAll", policy =>
                {
                    policy.SetIsOriginAllowed(_ => true)
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials();
                });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            #if DEBUG
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger().UseSwaggerUI(s =>
                {
                    s.SwaggerEndpoint("/swagger/JSLMes/swagger.json", "JSLMes 1.0.0.1");
                    s.RoutePrefix = string.Empty;

                });
            }
            #endif

            //app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseCors("AllowAll");

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
